package ca.etsmtl.log430.lab1;

import java.io.PipedReader;
import java.io.PipedWriter;

/**************************************************************************************
 ** Class name: CourseFilter
 ** Original author: A.J. Lattanze, CMU
 ** Date: 12/3/99
 ** Version 1.0
 **
 ** Adapted by R. Champagne, Ecole de technologie superieure
 ** 2002-May-08, 2011-Jan-12
 **
 ***************************************************************************************
 ** Purpose: Assignment 1 for LOG430, Architectures logicielle. This
 ** assignment is designed to illustrate a pipe and filter architecture.  For the 
 ** instructions, refer to the assignment write-up.
 **
 ** Abstract: This class is intended to be a filter that will key on a particular course
 **	     number that is provided at instantiation.  Note that the stream has to be
 **	     buffered so that it can be checked to see if the specified course number 
 **	     appears on the steam.  If this string appears in the stream from MainFilter, 
 **	     it will be passed to the merge filter for output to a file.  If this course 
 **	     string does not appear in the pipe stream, then it is not passed to merge 
 **	     filter.
 ** 
 ** Pseudo Code:
 **
 ** 	connect to input pipe
 ** 	connect to output pipe
 **
 **	while not end of line
 **
 **		read input pipe
 **
 **		if course string specified appears on line of text
 **
 **			write line of text to output pipe
 **			flush pipe
 **
 **		end if
 **
 **	end while
 **
 **	close pipes
 **
 ** Running the program
 **
 ** 	See MainFilter.java
 **
 ** Modification Log
 **************************************************************************************
 **
 **************************************************************************************/


public class CourseFilter extends Thread {

	// Declarations

	boolean Done;

	String CourseNumberString;

	// Make an input pipe to that will connect to MainFilter filter

	PipedReader InputPipe = new PipedReader();

	PipedWriter OutputPipeSuccess = new PipedWriter();

	PipedWriter OutputPipeFail = new PipedWriter();

	public CourseFilter(String CourseNumberString, PipedWriter InputPipe,
			PipedWriter OutputPipeSuccess, PipedWriter OutputPipeFail) {

		this.CourseNumberString = CourseNumberString;

		try {

			// Connect InputPipe to MainFilter

			this.InputPipe.connect(InputPipe);
			System.out.println("CourseFilter " + CourseNumberString
					+ ":: connected to upstream filter.");

			// Connect OutputPipe to Merge

			this.OutputPipeSuccess = OutputPipeSuccess;
			

			this.OutputPipeFail = OutputPipeFail;
			
			System.out.println("CourseFilter " + CourseNumberString
					+ ":: connected to downstream filter.");
			
		} catch (Exception Error) {

			System.out.println("CourseFilter " + CourseNumberString
					+ ":: Error connecting to other filters.");

		} // try/catch

	} // Constructor

	// This is the method that is called when the thread is started in
	// MainFilter

	public void run() {

		// Declarations

		char[] CharacterValue = new char[1];
		// char array is required to turn char into a string
		String LineOfText = "";
		// string is required to look for the course number
		int IntegerCharacter; // the integer value read from the pipe

		try {

			Done = false;

			while (!Done) {

				IntegerCharacter = InputPipe.read();
				CharacterValue[0] = (char) IntegerCharacter;

				if (IntegerCharacter == -1) { // pipe is closed

					Done = true;

				} else {

					if (IntegerCharacter == '\n') { // end of line

						System.out.println("CourseFilter " + CourseNumberString
								+ ":: received: " + LineOfText + ".");

						if (LineOfText.indexOf(CourseNumberString) != -1) {

							System.out.println("CourseFilter "
									+ CourseNumberString + ":: sending: "
									+ LineOfText + " to output success pipe.");
							LineOfText += new String(CharacterValue);
													
							OutputPipeSuccess
									.write(LineOfText, 0, LineOfText.length());
							OutputPipeSuccess.flush();
							
						} else {
							//Si pas dans la liste envoye au fail pipe	
							System.out.println("CourseFilter "
									+ CourseNumberString + ":: sending: "
									+ LineOfText + " to output fail pipe.");
							LineOfText += new String(CharacterValue);
													
							OutputPipeFail
									.write(LineOfText, 0, LineOfText.length());
							OutputPipeFail.flush();
						}

						LineOfText = "";

					} else {

						LineOfText += new String(CharacterValue);

					} // if //

				} // if

			} // while

		} catch (Exception Error) {

			System.out.println("CourseFilter::" + CourseNumberString
					+ " Interrupted.");

		} // try/catch

		try {

			InputPipe.close();
			System.out.println("CourseFilter " + CourseNumberString
					+ ":: input pipe closed.");

			OutputPipeSuccess.close();
			System.out.println("CourseFilter " + CourseNumberString
					+ ":: output success pipe closed.");
			
			OutputPipeFail.close();
			System.out.println("CourseFilter " + CourseNumberString
					+ ":: output fail pipe closed.");


		} catch (Exception Error) {

			System.out.println("CourseFilter " + CourseNumberString
					+ ":: Error closing pipes.");

		} // try/catch

	} // run

} // class
